package com.taobao.tddl;

import com.taobao.diamond.common.Constants;
import com.taobao.tddl.common.exception.TddlException;
import com.taobao.tddl.common.utils.GeneralUtil;
import com.taobao.tddl.matrix.jdbc.TDataSource;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * Created by qibaichao on 2015/5/25.
 */
public class DataSource extends TDataSource {

    public static void main(String args[]) throws Exception {
       // String rootClassPath = GeneralUtil.class.getResource("/").getPath();
       // System.out.println(rootClassPath);
        testDataSource();

    }

    public static void testDataSource() throws Exception {

        Constants.HTTP_URI_FILE = "/diamond-server/config.co";
        TDataSource ds = new TDataSource();
        ds.setAppName("haqiaolong");
        ds.setDynamicRule(true);
        //ds.setTopologyFile("matrix/server_topology.xml");
        ds.setSchemaFile("matrix/mysql_schema.xml");
        //ds.setRuleFile("matrix/mysql_rule.xml");
        ds.init();

        Connection conn = ds.getConnection();
        PreparedStatement ps = conn.prepareStatement("select * from haqiaolong where id=15262");
        ResultSet rs = ps.executeQuery();
        System.out.println(rs.next());

        ps = conn.prepareStatement("select count(1) from tddl limit 10");
        rs = ps.executeQuery();
//        Assert.assertTrue(rs.next());

        rs.close();
        ps.close();
        conn.close();
    }


    public void doInit() throws TddlException {
        Constants.HTTP_URI_FILE = "/diamond-server/config.co";
        super.doInit();
    }
}
